import { defineStore } from 'pinia'
import { ref, reactive } from 'vue'
import { getPdfList } from '@/api'
import { download } from '@/utils/file'
export const usePdfListStore = defineStore('pdfList', () => {
  let initedBannerList = ref(false)
  let bannerList = ref([])
  const initPdfListByStore = () => {
    if (!initedBannerList.value) {
      getPdfList().then((r) => {

        bannerList.value = r
      })
    }
  }

  const downloadPdf = (pdf) => {

    // openning pdf file



    const pdf_name = bannerList.value[pdf].pdf_name || bannerList.value[pdf].remark

    return new Promise((resolve, reject) => {


      if (!bannerList.value[pdf].pdf_url) {
        reject(new Error('invalid pdf url'))
        return
      }


      download(pdf_name, bannerList.value[pdf].pdf_url).then(() => {
        resolve()
      }).catch((err) => {
        reject(err)
      })

    }
    )


  }
  return { pdfList: bannerList, initPdfListByStore, downloadPdf, download }
})

